package com.c78.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.c78.fz.mapper.HistoryMapper;
import com.c78.fz.common.result.Result;
import com.c78.fz.model.entity.History;
import com.c78.fz.service.HistoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class HistoryServiceImpl implements HistoryService {
    @Resource
    HistoryMapper historyMapper;

    @Override
    public Result getHistoryPage(String waterManName, String sendWaterTime, Integer pageNum, Integer pageSize) {
        if(sendWaterTime!=null&&!sendWaterTime.equals("")){
            sendWaterTime = sendWaterTime.substring(0,10);
        }
        Page<History> page = Page.of(pageNum,pageSize);
        historyMapper.selectPage(page,waterManName,sendWaterTime);
        return Result.ok(page);
    }

    @Override
    public Result saveHistory(History history) {
        if(historyMapper.insert(history)>0){
            return Result.ok("添加送水历史信息成功");
        }
        return Result.error("添加送水历史信息失败");
    }

    @Override
    public Result updateHistory(History history) {
        if(historyMapper.updateById(history)>0){
            return Result.ok("修改送水历史信息成功");
        }
        return Result.error("修改送水历史信息失败");
    }

    @Override
    public Result getHistoryById(Integer id) {
        LambdaQueryWrapper<History> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(History::getId,id);
        History history = historyMapper.selectOne(queryWrapper);
        return Result.ok(history);
    }

    @Override
    public Result batchDeleteHistory(List<Integer> ids) {
        if(historyMapper.delById(ids)>0){
            return Result.ok("删除送水历史信息成功");
        }
        return Result.error("删除送水历史信息失败");
    }
}
